/*
 * Copyright 2013 NJUT  qixiaobo. All rights reserved.
 */
package com.fujitsu.nanjing.tieba.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fujitsu.nanjing.tieba.base.BaseService;
import com.fujitsu.nanjing.tieba.constrant.CommonParamter;
import com.fujitsu.nanjing.tieba.ibator.CoreCreditsLog;
import com.fujitsu.nanjing.tieba.ibator.CoreCreditsLogCriteria;
import com.fujitsu.nanjing.tieba.ibator.CoreUser;
import com.fujitsu.nanjing.tieba.model.CreditsLog;

/**
 * CreditsLog.java
 * 
 * @author Administrator
 */
@Service
@Transactional
public class CreditsLogService
		extends
			BaseService<CoreCreditsLog, CoreCreditsLogCriteria> {
	@Resource
	private CreditsLog creditsLog;
	@Resource
	private UserService userService;
	@Override
	public CoreCreditsLog findById(String id) {
		return creditsLog.selectByPrimaryKey(Integer.parseInt(id));
	}
	/**
	 * 此方法也包含对积分的增减
	 * */
	@Override
	public CoreCreditsLog insert(CoreCreditsLog t) {
		creditsBusiness(t);
		creditsLog.insertSelective(t);
		return t;
	}

	@Override
	public void update(CoreCreditsLog t) {
		creditsLog.updateByPrimaryKeySelective(t);

	}

	@Override
	public void delete(CoreCreditsLog t) {
		creditsLog.deleteByPrimaryKey(t.getLogid());
	}
	public List<CoreCreditsLog> findByUseridWithPageNum(String userid,
			int offset) {
		CoreCreditsLogCriteria example = new CoreCreditsLogCriteria();
		example.setMysqlLength(CommonParamter.默认分页长度);
		if (offset > 0) {
			example.setMysqlOffset((offset - 1) * CommonParamter.默认分页长度);
		}
		example.setOrderByClause(CommonParamter.默认排序条件);
		example.createCriteria().andUseridEqualTo(userid);
		return creditsLog.selectByExample(example);
	}
	public int countByUserId(String userId) {
		CoreCreditsLogCriteria example = new CoreCreditsLogCriteria();
		example.createCriteria().andUseridEqualTo(userId);
		return creditsLog.countByExample(example);
	}
	/*
	 * 根据用户获取<code>CreditsLog</code>总页数
	 */
	public int findPageCountByUserId(String userId) {
		int pageCount = (int) Math.ceil((double) countByUserId(userId)
				/ CommonParamter.默认分页长度);
		if (pageCount == 0) {
			pageCount = 1;
		}
		return pageCount;
	}

	public List<CoreCreditsLog> findByFromUserid(String fromUser, int offset) {
		CoreCreditsLogCriteria example = new CoreCreditsLogCriteria();
		example.setMysqlLength(CommonParamter.默认分页长度);
		if (offset > 0) {
			example.setMysqlOffset((offset - 1) * CommonParamter.默认分页长度);
		}
		example.setOrderByClause(CommonParamter.默认排序条件);
		example.createCriteria().andFromuserEqualTo(fromUser);
		return creditsLog.selectByExample(example);
	}
	public int countByFromUserid(String fromUser) {
		CoreCreditsLogCriteria example = new CoreCreditsLogCriteria();
		example.createCriteria().andFromuserEqualTo(fromUser);
		return creditsLog.countByExample(example);
	}
	/*
	 * 根据fromUser获取<code>CreditsLog</code>总页数
	 */
	public int findPageCountByFromUser(String fromUser) {
		int pageCount = (int) Math.ceil((double) countByFromUserid(fromUser)
				/ CommonParamter.默认分页长度);
		if (pageCount == 0) {
			pageCount = 1;
		}
		return pageCount;
	}
	private void creditsBusiness(CoreCreditsLog t) {
		CoreUser fromUser = userService.findById(t.getFromuser());
		CoreUser user = userService.findById(t.getUserid());
		userService.addCredits(fromUser, t.getCredits() * -1);
		userService.addCredits(user, t.getCredits());
		userService.update(user);
		userService.update(fromUser);
	}

}
